package com.lineage.server.model;

import com.lineage.config.ConfigOther;
import com.lineage.server.datatables.sql.CharacterTable;
import com.lineage.server.model.Instance.L1PcInstance;
import com.lineage.server.serverpackets.S_PacketBox;
import com.lineage.server.serverpackets.ServerBasePacket;
import com.lineage.server.world.World;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class L1Clan {
  private static final Log _log = LogFactory.getLog(L1Clan.class);
  
  private final Lock _lock = new ReentrantLock(true);
  
  public static final int CLAN_RANK_LEAGUE_PUBLIC = 2;
  
  public static final int CLAN_RANK_LEAGUE_VICEPRINCE = 3;
  
  public static final int CLAN_RANK_LEAGUE_PRINCE = 4;
  
  public static final int CLAN_RANK_LEAGUE_PROBATION = 5;
  
  public static final int CLAN_RANK_LEAGUE_GUARDIAN = 6;
  
  public static final int CLAN_RANK_PUBLIC = 7;
  
  public static final int CLAN_RANK_PROBATION = 8;
  
  public static final int CLAN_RANK_GUARDIAN = 9;
  
  public static final int CLAN_RANK_PRINCE = 10;
  
  private int _clanId;
  
  private String _clanName;
  
  private int _leaderId;
  
  private String _leaderName;
  
  private int _castleId;
  
  private int _houseId;
  
  private int _warehouse = 0;
  
  private int _ClanSkillId;
  
  private int _ClanSkillLv;
  
  private int _clanadena;
  
  private final L1DwarfForClanInventory _dwarfForClan = new L1DwarfForClanInventory(this);
  
  private final ArrayList<String> _membersNameList = new ArrayList<>();
  
  private int _emblemId;
  
  String[] _rank = new String[] { 
      "", "", "[联盟一般]", "[联盟副君主]", "[联盟君主]", "[联盟修习骑士]", "[联盟守护骑士]", "[一般]", "[修习骑士]", 
      "[守护骑士]", 
      "[君主]" };
  
  private boolean _clanskill = false;
  
  private Timestamp _skilltime = null;
  
  private int _maxuser;
  
  private Timestamp _foundDate;
  
  private String _announcement;
  
  private int _emblemStatus;
  
  private int _clanLevel;
  
  private int _clanContribution;
  
  public int getClanId() {
    return this._clanId;
  }
  
  public void setClanId(int clan_id) {
    this._clanId = clan_id;
  }
  
  public String getClanName() {
    return this._clanName;
  }
  
  public void setClanName(String clan_name) {
    this._clanName = clan_name;
  }
  
  public int getLeaderId() {
    return this._leaderId;
  }
  
  public void setLeaderId(int leader_id) {
    this._leaderId = leader_id;
  }
  
  public String getLeaderName() {
    return this._leaderName;
  }
  
  public void setLeaderName(String leader_name) {
    this._leaderName = leader_name;
  }
  
  public int getCastleId() {
    return this._castleId;
  }
  
  public void setCastleId(int hasCastle) {
    this._castleId = hasCastle;
  }
  
  public int getHouseId() {
    return this._houseId;
  }
  
  public void setHouseId(int hasHideout) {
    this._houseId = hasHideout;
  }
  
  public void CheckClan_Exp20(L1PcInstance leavePc) {
    L1PcInstance[] clanMembers = getOnlineClanMember();
    int OnlineMemberSize = getOnlineClanMemberSize();
    if (leavePc != null && leavePc.hasSkillEffect(8070)) {
      leavePc.removeNoTimerSkillEffect(8070);
      leavePc.add_exp(-ConfigOther.clancountexp);
      leavePc.sendPackets((ServerBasePacket)new S_PacketBox(165, 0));
    } 
    if (OnlineMemberSize >= ConfigOther.clancount) {
      L1PcInstance[] array;
      int n = (array = clanMembers).length;
      int i = 0;
      while (i < n) {
        L1PcInstance clanMember = array[i];
        if (!clanMember.hasSkillEffect(8070)) {
          clanMember.setSkillEffect(8070, 0);
          clanMember.add_exp(ConfigOther.clancountexp);
          clanMember.sendPackets((ServerBasePacket)new S_PacketBox(165, 1));
        } 
        i++;
      } 
    } else {
      L1PcInstance[] array;
      int n = (array = clanMembers).length;
      int i = 0;
      while (i < n) {
        L1PcInstance clanMember = array[i];
        if (clanMember.hasSkillEffect(8070)) {
          clanMember.removeNoTimerSkillEffect(8070);
          clanMember.add_exp(-ConfigOther.clancountexp);
          clanMember.sendPackets((ServerBasePacket)new S_PacketBox(165, 0));
        } 
        i++;
      } 
    } 
  }
  
  public void addMemberName(String member_name) {
    this._lock.lock();
    try {
      if (!this._membersNameList.contains(member_name))
        this._membersNameList.add(member_name); 
      CheckClan_Exp20(null);
    } finally {
      this._lock.unlock();
    } 
  }
  
  public void delMemberName(String member_name) {
    this._lock.lock();
    try {
      if (this._membersNameList.contains(member_name))
        this._membersNameList.remove(member_name); 
      L1PcInstance leavePc = World.get().getPlayer(member_name);
      CheckClan_Exp20(leavePc);
    } finally {
      this._lock.unlock();
    } 
  }
  
  public int getOnlineClanMemberSize() {
    int count = 0;
    try {
      Iterator<String> iterator = this._membersNameList.iterator();
      while (iterator.hasNext()) {
        String name = iterator.next();
        L1PcInstance pc = World.get().getPlayer(name);
        if (pc != null)
          count++; 
      } 
    } catch (Exception e) {
      _log.error(e.getLocalizedMessage(), e);
    } 
    return count;
  }
  
  public int getAllMembersSize() {
    try {
      return this._membersNameList.size();
    } catch (Exception e) {
      _log.error(e.getLocalizedMessage(), e);
      return 0;
    } 
  }
  
  public void sendPacketsAll(ServerBasePacket packet) {
    try {
      Object[] array;
      int length = (array = this._membersNameList.toArray()).length;
      int i = 0;
      while (i < length) {
        Object nameobj = array[i];
        String name = (String)nameobj;
        L1PcInstance pc = World.get().getPlayer(name);
        if (pc != null)
          pc.sendPackets(packet); 
        i++;
      } 
    } catch (Exception e) {
      _log.error(e.getLocalizedMessage(), e);
    } 
  }
  
  public L1PcInstance[] getOnlineClanMember() {
    ArrayList<String> temp = new ArrayList<>();
    ArrayList<L1PcInstance> onlineMembers = new ArrayList<>();
    try {
      temp.addAll(this._membersNameList);
      Iterator<String> iterator = temp.iterator();
      while (iterator.hasNext()) {
        String name = iterator.next();
        L1PcInstance pc = World.get().getPlayer(name);
        if (pc != null && !onlineMembers.contains(pc))
          onlineMembers.add(pc); 
      } 
    } catch (Exception e) {
      _log.error(e.getLocalizedMessage(), e);
    } 
    return onlineMembers.<L1PcInstance>toArray(new L1PcInstance[onlineMembers.size()]);
  }
  
  public StringBuilder getOnlineMembersFP() {
    ArrayList<String> temp = new ArrayList<>();
    StringBuilder result = new StringBuilder();
    try {
      temp.addAll(this._membersNameList);
      Iterator<String> iterator = temp.iterator();
      while (iterator.hasNext()) {
        String name = iterator.next();
        L1PcInstance pc = World.get().getPlayer(name);
        if (pc != null)
          result.append(String.valueOf(String.valueOf(name)) + " "); 
      } 
    } catch (Exception e) {
      _log.error(e.getLocalizedMessage(), e);
    } 
    return result;
  }
  
  public StringBuilder getAllMembersFP() {
    ArrayList<String> temp = new ArrayList<>();
    StringBuilder result = new StringBuilder();
    try {
      temp.addAll(this._membersNameList);
      Iterator<String> iterator = temp.iterator();
      while (iterator.hasNext()) {
        String name = iterator.next();
        result.append(String.valueOf(String.valueOf(name)) + " ");
      } 
    } catch (Exception e) {
      _log.error(e.getLocalizedMessage(), e);
    } 
    return result;
  }
  
  public int getEmblemId() {
    return this._emblemId;
  }
  
  public void setEmblemId(int i) {
    this._emblemId = i;
  }
  
  public StringBuilder getOnlineMembersFPWithRank() {
    ArrayList<String> temp = new ArrayList<>();
    StringBuilder result = new StringBuilder();
    try {
      temp.addAll(this._membersNameList);
      Iterator<String> iterator = temp.iterator();
      while (iterator.hasNext()) {
        String name = iterator.next();
        L1PcInstance pc = World.get().getPlayer(name);
        if (pc != null)
          result.append(String.valueOf(String.valueOf(name)) + getRankString(pc) + " "); 
      } 
    } catch (Exception e) {
      _log.error(e.getLocalizedMessage(), e);
    } 
    return result;
  }
  
  public StringBuilder getAllMembersFPWithRank() {
    ArrayList<String> temp = new ArrayList<>();
    StringBuilder result = new StringBuilder();
    try {
      temp.addAll(this._membersNameList);
      Iterator<String> iterator = temp.iterator();
      while (iterator.hasNext()) {
        String name = iterator.next();
        L1PcInstance pc = CharacterTable.get().restoreCharacter(name);
        if (pc != null)
          result.append(String.valueOf(String.valueOf(name)) + " "); 
      } 
    } catch (Exception e) {
      _log.error(e.getLocalizedMessage(), e);
    } 
    return result;
  }
  
  private String getRankString(L1PcInstance pc) {
    if (pc != null && pc.getClanRank() > 0)
      return this._rank[pc.getClanRank()]; 
    return "";
  }
  
  public String[] getAllMembers() {
    return this._membersNameList.<String>toArray(new String[this._membersNameList.size()]);
  }
  
  public L1DwarfForClanInventory getDwarfForClanInventory() {
    return this._dwarfForClan;
  }
  
  public int getWarehouseUsingChar() {
    return this._warehouse;
  }
  
  public void setWarehouseUsingChar(int objid) {
    this._warehouse = objid;
  }
  
  public void set_clanskill(boolean boolean1) {
    this._clanskill = boolean1;
  }
  
  public boolean isClanskill() {
    return this._clanskill;
  }
  
  public void set_skilltime(Timestamp skilltime) {
    this._skilltime = skilltime;
  }
  
  public Timestamp get_skilltime() {
    return this._skilltime;
  }
  
  public Timestamp getFoundDate() {
    return this._foundDate;
  }
  
  public void setFoundDate(Timestamp _foundDate) {
    this._foundDate = _foundDate;
  }
  
  public int getOnlineMaxUser() {
    return this._maxuser;
  }
  
  public void setOnlineMaxUser(int i) {
    this._maxuser = i;
  }
  
  public String getAnnouncement() {
    return this._announcement;
  }
  
  public void setAnnouncement(String announcement) {
    this._announcement = announcement;
  }
  
  public int getEmblemStatus() {
    return this._emblemStatus;
  }
  
  public void setEmblemStatus(int emblemStatus) {
    this._emblemStatus = emblemStatus;
  }
  
  public int getClanLevel() {
    return this._clanLevel;
  }
  
  public void setClanLevel(int clanLevel) {
    this._clanLevel = clanLevel;
  }
  
  public int getClanContribution() {
    return this._clanContribution;
  }
  
  public void setClanContribution(int clanContribution) {
    this._clanContribution = clanContribution;
  }
  
  public int getClanSkillId() {
    return this._ClanSkillId;
  }
  
  public void setClanSkillId(int i) {
    this._ClanSkillId = i;
  }
  
  public int getClanSkillLv() {
    return this._ClanSkillLv;
  }
  
  public void setClanSkillLv(int i) {
    this._ClanSkillLv = i;
  }
  
  public ArrayList<L1PcInstance> getOnlineMemberList() {
    ArrayList<L1PcInstance> onlineMembers = new ArrayList<>();
    Iterator<String> iterator = this._membersNameList.iterator();
    while (iterator.hasNext()) {
      String name = iterator.next();
      L1PcInstance pc = World.get().getPlayer(name);
      if (pc != null && !onlineMembers.contains(pc))
        onlineMembers.add(pc); 
    } 
    return onlineMembers;
  }
  
  public int getclanadena() {
    return this._clanadena;
  }
  
  public void setclanadena(int clanadena) {
    this._clanadena = clanadena;
  }
}
